package com.chinackts.auth.repository.redis;

import com.chinackts.auth.model.MenuType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author Lianhong
 * @since 2021/10/28
 * 解密JS仓库
 */
@Repository
public class MenuTypeRepository {

    @Resource
    private StringRedisTemplate redisTemplate1;

    private static final String KEY = "%s:menu-type:%s";

    @Value("${e-port.redis.timeout}")
    private long timeout;

    /**
     * 获取解密js
     *
     * @param id 用户标识
     */
    public String getMenuType(String id, MenuType type) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        String menuType = ops.get(String.format(KEY, id, type.getCode()));
        if (StringUtils.isEmpty(menuType)) return null;
        return menuType;
    }

    public void saveMenuType(String id, String menuTypeStr, MenuType type) {
        ValueOperations<String, String> ops = this.redisTemplate1.opsForValue();
        ops.set(String.format(KEY, id, type.getCode()), menuTypeStr, timeout, TimeUnit.HOURS);
    }

    public boolean exists(String id, MenuType type) {
        return Boolean.TRUE.equals(redisTemplate1.hasKey(String.format(KEY, id, type.getCode())));
    }
}
